package com.dataseek.iot.toolikit.common.util;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {
    /**
     * 常用变量
     */
    public static final String DATE_FORMAT_FULL = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_FULL_1 = "yyyy/M/dd H:mm";
    public static final String DATE_FORMAT_YMD = "yyyy-MM-dd";
    public static final String DATE_FORMAT_HMS = "HH:mm:ss";
    public static final String DATE_FORMAT_HM = "HH:mm";
    public static final String DATE_FORMAT_YMDHM = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_YMDHMS = "yyyyMMddHHmmss";
    public static final String TIME_YEAR = "year";
    public static final String TIME_MONTH = "month";
    public static final String TIME_DAY = "day";
    public static final String TIME_HOUR = "hour";
    public static final String TIME_MINUTE = "minute";
    public static final String TIME_SECONDS = "second";

    /**
     * 30天的毫秒數 = 30*24*60*60*1000
     */
    public static final long DAYS_30 = 2592000000L;

    /**
     * 日期转换为制定格式字符串
     *
     * @param time
     * @param format
     * @return
     */
    public static String formatDateToString(Date time, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(time);
    }

    //得到当前时间的yyyy-MM-dd HH:mm:ss字符串格式
    public static String getDateTimeNowStr(){
        return formatDateToString(new Date(), DateUtils.DATE_FORMAT_FULL);
    }

    //根据给定时间毫秒数转化为yyyy-MM-dd HH:mm:ss字符串格式
    public static String formatDateTimeStrFromMills(long timemills){
        Date date = new Date();
        date.setTime(timemills);
        return formatDateToString(date, DateUtils.DATE_FORMAT_FULL);
    }

    //根据给定时间毫秒数转化为yyyy-MM-dd HH:mm:ss字符串格式
    public static String fixFormatDateTimeStrFromMills(long timemills,String format){
        Date date = new Date();
        date.setTime(timemills);
        return formatDateToString(date, format);
    }


    /**
     * 字符串转换为制定格式日期
     * (注意：当你输入的日期是2014-12-21 12:12，format对应的应为yyyy-MM-dd HH:mm
     * 否则异常抛出)
     * @param date
     * @param format
     * @return
     * @throws ParseException
     *       @
     */
    public static Date formatStringToDate(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.parse(date);
        } catch (ParseException ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex.toString());
        }
    }

    /**
     * 提取输入日期相差输入时间的time
     */
    public static String getAddTimeFormat(String date,String type,int num,String format){
        Calendar cal = Calendar.getInstance();
        if (type == "day"){
            cal.add(Calendar.DATE,num);
        }else if (type == "month"){
            cal.add(Calendar.MONTH,num);
        }else if (type == "year"){
            cal.add(Calendar.YEAR,num);
        }else if (type == "minute"){
            cal.add(Calendar.MINUTE,num);
        }else if (type == "hour"){
            cal.add(Calendar.HOUR,num);
        }else if (type == "second"){
            cal.add(Calendar.SECOND,num);
        }else{
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        long time = cal.getTimeInMillis();
        String s = DateUtils.formatDateTimeStrFromMills(time);
        return s;
    }

    /**
     * 获取指定时间输入时间差的时间
     * @param date
     * @param type
     * @param num
     * @return
     */
    public static String getAddTime(Date date,String type,int num){
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        switch (type){
            case TIME_DAY:
                cal.add(Calendar.DATE,num);
                break;
            case TIME_MONTH:
                cal.add(Calendar.MONTH,num);
                break;
            case TIME_SECONDS:
                cal.add(Calendar.SECOND,num);
                break;
            case TIME_MINUTE:
                cal.add(Calendar.MINUTE,num);
                break;
            case TIME_HOUR:
                cal.add(Calendar.HOUR_OF_DAY,num);
                break;
            case TIME_YEAR:
                cal.add(Calendar.YEAR,num);
                break;
        }
        String s = DateUtils.formatDateTimeStrFromMills(cal.getTimeInMillis());
        return s;
    }
    /**
     * yyyy-MM-dd格式日期相減
     * (注意：当你输入的日期是2014-12-21 12:12，format对应的应为yyyy-MM-dd HH:mm
     * 否则异常抛出)
     * @param days
     * @return
     */
    public static String dateStringsub(long days) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime dt = now.minusDays(days);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String compareDate = dateTimeFormatter.format(dt);
        return compareDate;
    }

    public static Timestamp stringToTimestamp(String time) {
        Timestamp ts = new Timestamp(System.currentTimeMillis());
        try {
            ts = Timestamp.valueOf(time);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ts;
    }

}
